package o;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.j;
import androidx.recyclerview.widget.RecyclerView.o;
import androidx.recyclerview.widget.RecyclerView.s;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.concurrent.TimeUnit;

public final class md implements Runnable {
    static Comparator<d> ˊ = new Comparator<d>() {
        public /* synthetic */ int compare(Object obj, Object obj2) {
            return ˏ((d) obj, (d) obj2);
        }

        public int ˏ(d dVar, d dVar2) {
            int i = -1;
            if ((dVar.ॱ == null ? 1 : 0) != (dVar2.ॱ == null ? 1 : 0)) {
                return dVar.ॱ == null ? 1 : -1;
            } else {
                if (dVar.ˏ != dVar2.ˏ) {
                    if (!dVar.ˏ) {
                        i = 1;
                    }
                    return i;
                }
                int i2 = dVar2.ˋ - dVar.ˋ;
                if (i2 != 0) {
                    return i2;
                }
                i2 = dVar.ˊ - dVar2.ˊ;
                if (i2 == 0) {
                    return 0;
                }
                return i2;
            }
        }
    };
    public static final ThreadLocal<md> ˏ = new ThreadLocal();
    long ˋ;
    ArrayList<RecyclerView> ˎ = new ArrayList();
    public long ॱ;
    private ArrayList<d> ॱॱ = new ArrayList();

    public static class b implements androidx.recyclerview.widget.RecyclerView.j.b {
        int ˋ;
        int[] ˎ;
        int ˏ;
        int ॱ;

        void ॱ(int i, int i2) {
            this.ॱ = i;
            this.ˋ = i2;
        }

        void ˋ(RecyclerView recyclerView, boolean z) {
            this.ˏ = 0;
            if (this.ˎ != null) {
                Arrays.fill(this.ˎ, -1);
            }
            j jVar = recyclerView.ˋॱ;
            if (recyclerView.ˊॱ != null && jVar != null && jVar.ॱᐝ()) {
                if (z) {
                    if (!recyclerView.ॱॱ.ˎ()) {
                        jVar.ˋ(recyclerView.ˊॱ.ˏ(), (androidx.recyclerview.widget.RecyclerView.j.b) this);
                    }
                } else if (!recyclerView.ˋˊ()) {
                    jVar.ˊ(this.ॱ, this.ˋ, recyclerView.ˋˊ, (androidx.recyclerview.widget.RecyclerView.j.b) this);
                }
                if (this.ˏ > jVar.ʾ) {
                    jVar.ʾ = this.ˏ;
                    jVar.ʽॱ = z;
                    recyclerView.ˋ.ˊ();
                }
            }
        }

        public void ˎ(int i, int i2) {
            if (i < 0) {
                throw new IllegalArgumentException("Layout positions must be non-negative");
            } else if (i2 < 0) {
                throw new IllegalArgumentException("Pixel distance must be non-negative");
            } else {
                int i3 = this.ˏ * 2;
                if (this.ˎ == null) {
                    this.ˎ = new int[4];
                    Arrays.fill(this.ˎ, -1);
                } else if (i3 >= this.ˎ.length) {
                    Object obj = this.ˎ;
                    this.ˎ = new int[(i3 * 2)];
                    System.arraycopy(obj, 0, this.ˎ, 0, obj.length);
                }
                this.ˎ[i3] = i;
                this.ˎ[i3 + 1] = i2;
                this.ˏ++;
            }
        }

        public boolean ˊ(int i) {
            if (this.ˎ == null) {
                return false;
            }
            int i2 = this.ˏ * 2;
            for (int i3 = 0; i3 < i2; i3 += 2) {
                if (this.ˎ[i3] == i) {
                    return true;
                }
            }
            return false;
        }

        public void ॱ() {
            if (this.ˎ != null) {
                Arrays.fill(this.ˎ, -1);
            }
            this.ˏ = 0;
        }
    }

    static class d {
        public int ˊ;
        public int ˋ;
        public int ˎ;
        public boolean ˏ;
        public RecyclerView ॱ;

        d() {
        }

        public void ˎ() {
            this.ˏ = false;
            this.ˋ = 0;
            this.ˊ = 0;
            this.ॱ = null;
            this.ˎ = 0;
        }
    }

    public void ˏ(RecyclerView recyclerView) {
        this.ˎ.add(recyclerView);
    }

    public void ˎ(RecyclerView recyclerView) {
        this.ˎ.remove(recyclerView);
    }

    public void ˎ(RecyclerView recyclerView, int i, int i2) {
        if (recyclerView.isAttachedToWindow() && this.ˋ == 0) {
            this.ˋ = recyclerView.ˊˊ();
            recyclerView.post(this);
        }
        recyclerView.ˉ.ॱ(i, i2);
    }

    private void ˎ() {
        int size = this.ˎ.size();
        int i = 0;
        int i2 = 0;
        while (i < size) {
            int i3;
            RecyclerView recyclerView = (RecyclerView) this.ˎ.get(i);
            if (recyclerView.getWindowVisibility() == 0) {
                recyclerView.ˉ.ˋ(recyclerView, false);
                i3 = recyclerView.ˉ.ˏ + i2;
            } else {
                i3 = i2;
            }
            i++;
            i2 = i3;
        }
        this.ॱॱ.ensureCapacity(i2);
        boolean z = false;
        for (int i4 = 0; i4 < size; i4++) {
            recyclerView = (RecyclerView) this.ˎ.get(i4);
            if (recyclerView.getWindowVisibility() == 0) {
                b bVar = recyclerView.ˉ;
                int abs = Math.abs(bVar.ॱ) + Math.abs(bVar.ˋ);
                boolean z2 = z;
                for (i = 0; i < bVar.ˏ * 2; i += 2) {
                    d dVar;
                    boolean z3;
                    if (z2 >= this.ॱॱ.size()) {
                        dVar = new d();
                        this.ॱॱ.add(dVar);
                    } else {
                        dVar = (d) this.ॱॱ.get(z2);
                    }
                    int i5 = bVar.ˎ[i + 1];
                    if (i5 <= abs) {
                        z3 = true;
                    } else {
                        z3 = false;
                    }
                    dVar.ˏ = z3;
                    dVar.ˋ = abs;
                    dVar.ˊ = i5;
                    dVar.ॱ = recyclerView;
                    dVar.ˎ = bVar.ˎ[i];
                    z2++;
                }
                z = z2;
            }
        }
        Collections.sort(this.ॱॱ, ˊ);
    }

    static boolean ˋ(RecyclerView recyclerView, int i) {
        int ˋ = recyclerView.ʼ.ˋ();
        for (int i2 = 0; i2 < ˋ; i2++) {
            s ˏ = RecyclerView.ˏ(recyclerView.ʼ.ˋ(i2));
            if (ˏ.ˎ == i && !ˏ.ॱˊ()) {
                return true;
            }
        }
        return false;
    }

    private s ˋ(RecyclerView recyclerView, int i, long j) {
        if (ˋ(recyclerView, i)) {
            return null;
        }
        o oVar = recyclerView.ˋ;
        try {
            recyclerView.ॱˋ();
            s ˋ = oVar.ˋ(i, false, j);
            if (ˋ != null) {
                if (!ˋ.ॱᐝ() || ˋ.ॱˊ()) {
                    oVar.ˊ(ˋ, false);
                } else {
                    oVar.ˊ(ˋ.ˊ);
                }
            }
            recyclerView.ॱ(false);
            return ˋ;
        } catch (Throwable th) {
            recyclerView.ॱ(false);
        }
    }

    private void ˏ(@Nullable RecyclerView recyclerView, long j) {
        if (recyclerView != null) {
            if (recyclerView.ʼॱ && recyclerView.ʼ.ˋ() != 0) {
                recyclerView.ˏ();
            }
            b bVar = recyclerView.ˉ;
            bVar.ˋ(recyclerView, true);
            if (bVar.ˏ != 0) {
                try {
                    gp.ˏ("RV Nested Prefetch");
                    recyclerView.ˋˊ.ˊ(recyclerView.ˊॱ);
                    for (int i = 0; i < bVar.ˏ * 2; i += 2) {
                        ˋ(recyclerView, bVar.ˎ[i], j);
                    }
                } finally {
                    gp.ˊ();
                }
            }
        }
    }

    private void ˋ(d dVar, long j) {
        s ˋ = ˋ(dVar.ॱ, dVar.ˎ, dVar.ˏ ? Long.MAX_VALUE : j);
        if (ˋ != null && ˋ.ॱ != null && ˋ.ॱᐝ() && !ˋ.ॱˊ()) {
            ˏ((RecyclerView) ˋ.ॱ.get(), j);
        }
    }

    private void ॱ(long j) {
        int i = 0;
        while (i < this.ॱॱ.size()) {
            d dVar = (d) this.ॱॱ.get(i);
            if (dVar.ॱ != null) {
                ˋ(dVar, j);
                dVar.ˎ();
                i++;
            } else {
                return;
            }
        }
    }

    void ˎ(long j) {
        ˎ();
        ॱ(j);
    }

    public void run() {
        try {
            gp.ˏ("RV Prefetch");
            if (!this.ˎ.isEmpty()) {
                int size = this.ˎ.size();
                int i = 0;
                long j = 0;
                while (i < size) {
                    long max;
                    RecyclerView recyclerView = (RecyclerView) this.ˎ.get(i);
                    if (recyclerView.getWindowVisibility() == 0) {
                        max = Math.max(recyclerView.getDrawingTime(), j);
                    } else {
                        max = j;
                    }
                    i++;
                    j = max;
                }
                if (j == 0) {
                    this.ˋ = 0;
                    gp.ˊ();
                    return;
                }
                ˎ(TimeUnit.MILLISECONDS.toNanos(j) + this.ॱ);
                this.ˋ = 0;
                gp.ˊ();
            }
        } finally {
            this.ˋ = 0;
            gp.ˊ();
        }
    }
}
